package com.erpanderp.wam.usm.model.service.user;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeRole;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class EmployeeRoleDAO {

	public EmployeeRole getEmployeeRole(Long employeeId, Long roleId) {
		EmployeeRole employeeRole = null;
		try {
			String status = CommonLookup.STATUS_ACTIVE;
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from EmployeeRole er where er.status = :status";
			
			if(employeeId != null && !employeeId.equals(0)){
				queryString += " and er.employee.employeeId = :employeeId";
			}
			
			if(roleId != null && !roleId.equals(0)){
				queryString += " and er.role.roleId = :roleId";
			}
			
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status", status);
			
			if(employeeId != null && !employeeId.equals(0)){
				query.setLong("employeeId", employeeId);
			}
			
			if(roleId != null && !roleId.equals(0)){
				query.setLong("roleId", roleId);
			}
			
			List<EmployeeRole> tempList = (List<EmployeeRole>) query.list();
			if (tempList != null && tempList.size() > 0){
				employeeRole = tempList.get(0);
			}
			session.getTransaction().commit();
			return employeeRole;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * Get Role By Passing Employee ID
	 * @param employeeId
	 * @return
	 */
	public Role getRoleByEmployeeId(Long employeeId) {
		Role role = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("select er.role from EmployeeRole er where er.employee.employeeId = :employeeId");
			query.setCacheable(false).setLong("employeeId", employeeId);
			List<Role> tempList = (List<Role>) query.list();
			if (tempList != null && tempList.size() > 0){
				role = tempList.get(0);
			}
			session.getTransaction().commit();
			return role;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<String> getAllProjectManagersEmailIds(){
		List<String> employeeEmailIds = new ArrayList<String>();
		try{
			System.out.println("............ProjectManagersEmailId...........");
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "select er.employee.email from EmployeeRole er where er.role.role = 'PM'";
			System.out.println("............select e.email from Employee e where e.roleId = 7...........");
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			employeeEmailIds =(List<String>) query.list();
			System.out.println("............select e.email from Employee e where e.roleId = 7..........."+employeeEmailIds.size());
			session.getTransaction().commit();
			return employeeEmailIds;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	public EmployeeRole updateEmployeeRole(EmployeeRole employeeRole){
		try{
		 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 session.beginTransaction();
		 session.setFlushMode(FlushMode.COMMIT);
		 session.saveOrUpdate(employeeRole);
		 session.getTransaction().commit();
		 return employeeRole;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public static void main(String a[]) {
	}

}
